package com.gxuwz.web.struts2;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import com.gxuwz.annotation.LogMsg;
import com.gxuwz.common.Const;
import com.gxuwz.entity.SysDepartment;
import com.gxuwz.entity.SysEquipment;
import com.gxuwz.entity.SysLaboratory;
import com.gxuwz.entity.SysRepairApplication;
import com.gxuwz.entity.SysUser;
import com.gxuwz.service.IDepartmentService;
import com.gxuwz.service.IEquipmentService;
import com.gxuwz.service.ILaboratoryService;
import com.gxuwz.service.IRepairApplicationService;
import com.gxuwz.util.DateUtils;
import com.gxuwz.util.Page;
import com.gxuwz.web.AbstractBaseController;
import com.gxuwz.web.IBaseController;

public class RepairApplicationController extends AbstractBaseController
		implements IBaseController {

	private static final long serialVersionUID = -655895588539113241L;

	private List<Integer> ids; // 获取复选框的值
	private String result; // 返回到客户端的数据

	private Integer labId;
	private Integer deptId;
	private Integer equipId;
	private SysRepairApplication repairApplication;

	@Resource(name = "departmentService")
	private IDepartmentService departmentService;
	@Resource(name = "laboratoryService")
	private ILaboratoryService laboratoryService;
	@Resource(name = "equipmentService")
	private IEquipmentService equipmentService;
	@Resource(name = "repairApplicationService")
	private IRepairApplicationService repairApplicationService;

	/**
	 * 获取部门
	 * 
	 * @return
	 */
	public String findDepartment() {
		List<SysDepartment> departments = departmentService.getAllFromTable();
		JsonConfig config = new JsonConfig();
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);// 防止因自包含出现死循环的问题
		config.setExcludes(new String[] { "laboratorys" }); // 过滤掉实验室
		JSONArray jArray = JSONArray.fromObject(departments, config);
		result = jArray.toString();
		return SUCCESS;
	}

	/**
	 * 获取实验室
	 * 
	 * @return
	 */
	public String findLaboratory() {
		List<SysLaboratory> laboratorys = laboratoryService
				.findLabByDeptId(deptId);
		JsonConfig config = new JsonConfig();
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);// 防止因自包含出现死循环的问题
		config.setExcludes(new String[] { "department", "equipments" }); // 过滤掉部门和设备
		JSONArray jArray = JSONArray.fromObject(laboratorys, config);
		result = jArray.toString();
		return SUCCESS;
	}

	/**
	 * 获取设备
	 * 
	 * @return
	 */
	public String findEquipment() {
		List<SysEquipment> equipments = equipmentService
				.findEquipByLabId(labId);
		JsonConfig config = new JsonConfig();
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); // 防止因自包含出现死循环的问题
		config.setExcludes(new String[] { "laboratory" }); // 过滤掉实验室
		JSONArray jArray = JSONArray.fromObject(equipments, config);
		result = jArray.toString();
		return SUCCESS;
	}

	/**
	 * 报修申请列表
	 */
	@Override
	public String listPrompt() {
		Page<SysRepairApplication> page = new Page<SysRepairApplication>(); // 创建分页对象指定泛型
		page.setOnePageNum(10); // 设置单页记录数
		page.setPageNo(pageNo == null ? 1 : pageNo); // 设置当前页数 默认1
		page.setClazz("RepairApplication");
		repairApplicationService.findForPage(page);
		request.put("page", page);
		// 判断是实验室技术员登陆还是检测人员登陆，显示不同的列表(目前先用examineListPrompt方法测试)
		setPrompt("/WEB-INF/pages/repair/repair_application_list.jsp");
		return SUCCESS;
	}

	/**
	 * 审核列表
	 * 
	 * @return
	 */
	public String examineListPrompt() {
		Page<SysRepairApplication> page = new Page<SysRepairApplication>(); // 创建分页对象指定泛型
		page.setOnePageNum(10); // 设置单页记录数 默认5
		page.setPageNo(pageNo == null ? 1 : pageNo); // 设置当前页数 默认1
		page.setClazz("RepairApplication");
		repairApplicationService.findForPage(page);
		request.put("page", page);
		// 判断是实验室技术员登陆还是检测人员登陆，显示不同的列表
		setPrompt("/WEB-INF/pages/repair/repair_examine_list.jsp");
		return SUCCESS;
	}

	@LogMsg(msg = "设备维修审核")
	public String examine() {
		SysUser user = (SysUser) session.get("user");
		repairApplication.setDealDate(DateUtils.getCurrentDate());
		repairApplication.setInspName(user.getUserName());
		repairApplicationService.update(repairApplication);
		setPrompt("RepairApplication_examineListPrompt.action");
		return "reposition";
	}

	@Override
	public String addPrompt() {
		setPrompt("/WEB-INF/pages/repair/repair_application.jsp");
		return SUCCESS;
	}

	@Override
	@LogMsg(msg = "设备维修申请")
	public String doSave() {
		SysUser user = (SysUser) session.get("user");
		repairApplication.setTechName(user.getUserName());
		SysDepartment department = departmentService.load(deptId);
		SysLaboratory laboratory = laboratoryService.load(labId);
		final String hql = "from SysRepairApplication where equip_id = ? and app_status = ?";
		// 如果该设备已经申请过报修并且没有审核就不能重复申请
		SysRepairApplication apply = repairApplicationService.getUniqueResult(
				hql, equipId, Const.NO_EXAMINE);

		// 弹窗提示
		if (apply != null) {
			bombWindow("添加失败，存在记录未检测！", "RepairApplication_addPrompt.action");
			return null;
		} else {
			repairApplication.setEquipId(equipId);
			repairApplication.setDeptName(department.getDeptName());
			repairApplication.setLabName(laboratory.getLabName());
			repairApplication.setCreateDate(DateUtils.getCurrentDate());
			repairApplicationService.save(repairApplication);
			setPrompt("RepairApplication_listPrompt.action");
			return "reposition";
		}
	}

	/**
	 * 弹窗
	 * 
	 * @return
	 */
	public void bombWindow(String msg, String href) {
		super.response.setContentType("text/html;charset=UTF-8");
		super.response.setCharacterEncoding("UTF-8");// 防止弹出的信息出现乱码
		try {
			PrintWriter out = response.getWriter();
			out.print("<script>alert('" + msg + "')</script>");
			out.print("<script>window.location.href='" + href + "'</script>");
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 填写审核信息页面
	 * 
	 * @return
	 */
	public String examinePrompt() {
		SysRepairApplication repair = repairApplicationService
				.load(repairApplication.getId());
		session.put("examineMsg", repair);
		setPrompt("/WEB-INF/pages/repair/repair_examine.jsp");
		return SUCCESS;
	}

	@Override
	@LogMsg(msg = "删除申请记录")
	public String doBatchDelete() {
		repairApplicationService.batchDelete(ids);
		setPrompt("RepairApplication_listPrompt.action");
		return "reposition";
	}

	@Override
	public String updatePrompt() {
		SysRepairApplication apply = repairApplicationService
				.load(repairApplication.getId());
		session.put("updateApply", apply);
		setPrompt("/WEB-INF/pages/repair/repair_application_update.jsp");
		return SUCCESS;
	}

	@Override
	public String doUpdate() {
		return null;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public Integer getLabId() {
		return labId;
	}

	public void setLabId(Integer labId) {
		this.labId = labId;
	}

	public Integer getDeptId() {
		return deptId;
	}

	public void setDeptId(Integer deptId) {
		this.deptId = deptId;
	}

	public Integer getEquipId() {
		return equipId;
	}

	public void setEquipId(Integer equipId) {
		this.equipId = equipId;
	}

	public SysRepairApplication getRepairApplication() {
		return repairApplication;
	}

	public void setRepairApplication(SysRepairApplication repairApplication) {
		this.repairApplication = repairApplication;
	}

	public List<Integer> getIds() {
		return ids;
	}

	public void setIds(List<Integer> ids) {
		this.ids = ids;
	}

}
